﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CanDoo;
using CanDoo.Core;
using CanDoo.Core.Data;
using CanDoo.Data;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Oil.Core.DTOs;
using CanDoo.Oil.Core.Models;
using System.Threading.Tasks;

namespace CanDoo.Oil.Core.Services
{
    /// <summary>
    /// 业务实现——API操作日志
    /// </summary>
    public class APIOperateLogService : CanDoo.Data.Entity.Services.ServiceBase<APIOperateLog>, IAPIOperateLogContract
    {
        /// <summary>
        /// 业务实现——售油机序列号
        /// </summary>
        public IOilSalesSerialNumContract OilSalesSerialNumContract { get; set; }

        /// <summary>
        /// 业务实现——油品序列号
        /// </summary>
        public IOilSerialNumContract OilSerialNumContract { get; set; }
        public IGetOilCodeContract GetOilCodeContract { get; set; }
        public ICustomerContract CustomerContract { get; set; }
        public IGetOilContract GetOilContract { get; set; }

        public IMoneyChangeContract MoneyChangeContract { get; set; }
        public ICleanRecordContract CleanRecordContract { get; set; }
        public APIOperateLogService()
        {
        
        }

        /// <summary>
        /// 1.1.4取油码取油状态上报:  终端完成取油操作时，将取油状态上报至后台
        /// </summary>
        /// <param name="status">取油状态信息</param>
        /// <returns>操作结果</returns>
        public async Task<OperationResult> AddOilCodeStatusAsync(OilCodeStatus status)
        {
            //memberId 应该没有意义  type有什么意义
            using (var scope = DbScopeFactory.Create())
            {
                OilSalesSerialNum oilSales = await OilSalesSerialNumContract.GetByTerminalNoAsync(status.terminalNo);
                if (oilSales == null)
                    return new OperationResult(OperationResultType.QueryNull, $"找不到终端号为{status.terminalNo}的售油机.");

                var oilSerialNum = OilSerialNumContract.SingleOrDefault(p => p.OilSalesSerialNumId == oilSales.Id && p.Status == OilSerialStatus.OnSale && p.OilPort == status.oilPort);
                if (oilSerialNum == null)
                    return new OperationResult(OperationResultType.QueryNull, $"此油口查不到油，可能参数有误");

                var getOilCode = GetOilCodeContract.SingleOrDefault(p => p.OilCode == status.oilCode);
                if (getOilCode == null)
                    return new OperationResult(OperationResultType.QueryNull, "取油码不存在");

                var codebefore = getOilCode.Lasted; //取油码原来有多少油
                var codelasted = getOilCode.Lasted - status.millilitre; //更新取油码还剩下多少油
                getOilCode.Lasted = codelasted;
                getOilCode.StoreNo = null;
                GetOilCodeContract.UpdateEntity(getOilCode);

                var before = oilSerialNum.LastOil;
                oilSerialNum.LastOil = oilSerialNum.LastOil - status.millilitre;
                if (oilSerialNum.LastOil < 0)
                    oilSerialNum.LastOil = 0;                                //更新这个油桶还剩下多少油
                var lasted = oilSerialNum.LastOil;
                OilSerialNumContract.UpdateEntity(oilSerialNum);


                var mEntity = new GetOil();
                mEntity.Name = "取油码[" + status.oilCode + "]在机器[" + status.terminalNo + "]油口[" + status.oilPort + "]取油[" + status.millilitre + "]g";
                mEntity.OilSalesSerialNumId = oilSales.Id;
                mEntity.OilSerialNumId = oilSerialNum.Id; 
                mEntity.OilPort = status.oilPort;
                mEntity.OperateType = 3;
                mEntity.OpreateTime = status.takeOilTime.ToDateTime();
                mEntity.OilOutPut = status.millilitre.ToDouble();
                mEntity.OilBefore = before;
                mEntity.OilAfter = lasted;

                mEntity.Price = oilSerialNum.Product.RetailPrice * 0.01;
                mEntity.Money = status.amount * 0.01 * -1;

                mEntity.LinkEntityId = getOilCode.Id;
                mEntity.MoneyBefore = codebefore;
                mEntity.MoneyAfter = codelasted;
                mEntity.MoneyChangeId = null;
                mEntity.CustomerId = null;
                GetOilContract.AddEntity(mEntity);


                //#region APIOperateLog
                //var apiOperateLog = new APIOperateLog();
                //apiOperateLog.Name = "取油码["+status.oilCode+"]在机器["+ status.terminalNo + "]油口["+status.oilPort+"]取油["+status.millilitre+"]KG";
                //apiOperateLog.OilSalerNo = status.terminalNo;
                //apiOperateLog.OilSalesSerialNumId = oilSales.Id;
                //apiOperateLog.OilPort = status.oilPort;
                //apiOperateLog.OilSerialNumId = oilSerialNum.Id;
                //apiOperateLog.BatchNo = "";
                //apiOperateLog.OpreateTime = status.takeOilTime.ToDateTime();
                //apiOperateLog.OperateType = 4;
                //apiOperateLog.OilOutPut = status.millilitre.ToDouble();
                //apiOperateLog.Before = before;
                //apiOperateLog.Last = lasted;
                //apiOperateLog.Price = status.amount / status.millilitre;
                //apiOperateLog.Money = status.amount;
                //apiOperateLog.CustomMoney = status.millilitre.ToDouble();
                //apiOperateLog.CustomMoneyLast = getOilCode.Lasted;
                //apiOperateLog.CustomMoneyBefore = apiOperateLog.CustomMoney + apiOperateLog.CustomMoneyLast;
                //apiOperateLog.UpdateTime = status.createTime.ToDateTime();
                //apiOperateLog.Token = "";
                //apiOperateLog.Operater = "test";
                //apiOperateLog.LinkEntityId = getOilCode.Id;
                //AddEntity(apiOperateLog);
                //#endregion

                //OperationResult result = OperationResult.NoChanged;

                var result = (await scope.SaveChangesAsync()) > 0 ? OperationResult.Success : OperationResult.NoChanged;
                return result;
            }
        }

        /// <summary>
        /// 1.1.10会员取油成功状态上报 : 会员取油成功后，将取油情况上报后台
        /// </summary>
        /// <param name="oilTankStatus">取油请求报文</param>
        /// <returns>操作结果</returns>
        public OperationResult AddOilTankStatus(OilTankStatus status)
        {
            //Type干嘛用
            using (var scope = DbScopeFactory.Create())
            {
                OperationResult result = OperationResult.NoChanged;

                if (status.type != 4 && status.type != 5)
                {
                    result.ResultType = OperationResultType.ValidError;
                    result.Message = "状态参数不正确";
                    return result;
                }

                var oilSales = OilSalesSerialNumContract.FirstOrDefault(p => p.TerminalNum == status.terminalNo);
                if (oilSales == null)
                {
                    result.ResultType = OperationResultType.ValidError;
                    result.Message = "未找到售油机";
                    return result;
                }

                var oilSerialNum = OilSerialNumContract.FirstOrDefault(p => p.OilSalesSerialNumId == oilSales.Id && p.Status== OilSerialStatus.OnSale && p.OilPort == status.oilPort);
                if (oilSerialNum == null)
                {
                    result.ResultType = OperationResultType.ValidError;
                    result.Message = "未找到对应的油";
                    return result;
                }

                if(oilSerialNum.Product.SN!=status.oilId)
                {
                    result.ResultType = OperationResultType.ValidError;
                    result.Message = "油品不对应";
                    return result;
                }

                if (status.type == 4 && status.cardNo == "000000")
                {
                    result.ResultType = OperationResultType.ValidError;
                    result.Message = "会员卡取油必须提供会员卡号";
                    return result;
                }

                Customer customer = null;
                if (status.cardNo !="000000")
                {
                    customer = CustomerContract.FirstOrDefault(p => p.CardNo == status.cardNo);
                    if (customer == null)
                    {
                        result.ResultType = OperationResultType.ValidError;
                        result.Message = "未找到客户";
                        return result;
                    }
                }

                var before = oilSerialNum.LastOil;
                oilSerialNum.LastOil = oilSerialNum.LastOil - status.millilitre;
                if (oilSerialNum.LastOil < 0)
                    oilSerialNum.LastOil = 0; //更新这个油桶还剩下多少油
                var lasted = oilSerialNum.LastOil;

                long? GroupId = null;
                long moneyChangeId;

                var nEntity = new MoneyChange();
                if (status.type == 5)
                {
                    nEntity = new MoneyChange();
                    nEntity.Id = IdMaker.NewId();
                    GroupId = nEntity.Id;
                    nEntity.MoneyType = 101;
                    nEntity.Code = status.voucherNo;
                    if (customer != null)
                    {
                        nEntity.UserId = customer.Id;
                        nEntity.Name = "客户[" + customer.CardNo + "]在终端[" + status.terminalNo + "]取油[" + status.millilitre + "]充值";
                        nEntity.MoneyBefore = customer.Balance;
                        nEntity.Money = status.amount * 0.01;
                        nEntity.MoneyAfter = customer.Balance + nEntity.Money;
                    }
                    else
                    {
                        nEntity.UserId = null;
                        nEntity.Name = "客户在终端[" + status.terminalNo + "]取油[" + status.millilitre + "]充值";
                        nEntity.MoneyBefore = 0;
                        nEntity.Money = status.amount * 0.01;
                        nEntity.MoneyAfter = status.amount * 0.01;
                    }
                    nEntity.GroupId = GroupId;
                    MoneyChangeContract.AddEntity(nEntity);
                    

                    nEntity = new MoneyChange();
                    nEntity.Id = IdMaker.NewId();
                    nEntity.MoneyType = 201;
                    nEntity.Code = "";
                    if (customer != null)
                    {
                        nEntity.UserId = customer.Id;
                        nEntity.Name = "客户[" + customer.CardNo + "]在终端[" + status.terminalNo + "]油口[" + status.oilPort + "]取油[" + status.millilitre + "]g";
                        nEntity.MoneyBefore = customer.Balance+ status.amount * 0.01; //因为充值了 所以之前的金额是余额+这次用掉的
                        nEntity.Money = status.amount * 0.01;
                        nEntity.MoneyAfter = customer.Balance;
                    }
                    else
                    {
                        nEntity.UserId = null;
                        nEntity.Name = "客户在终端[" + status.terminalNo + "]油口[" + status.oilPort + "]取油[" + status.millilitre + "]g";
                        nEntity.MoneyBefore = status.amount * 0.01;
                        nEntity.Money = status.amount * 0.01 * -1;
                        nEntity.MoneyAfter = 0;
                    }
                    nEntity.GroupId = GroupId;
                    MoneyChangeContract.AddEntity(nEntity);
                    moneyChangeId = nEntity.Id;

                    if (customer != null)
                    {
                        customer.StateUpdateTime = null;
                        customer.StoreNo = null;
                        customer.LastReportTime = status.takeOilTime;
                        CustomerContract.Update(customer);
                    }
                }
                else
                {
                    nEntity.Id = IdMaker.NewId();
                    nEntity.Name = "客户[" + customer.CardNo + "]在终端[" + status.terminalNo + "]油口[" + status.oilPort + "]取油[" + status.millilitre + "]g";
                    nEntity.UserId = customer.Id;
                    nEntity.MoneyType = 201;
                    nEntity.Code = "";
                    nEntity.MoneyBefore = customer.Balance;

                    if (customer.CardType != 2)
                    {
                        nEntity.Money = status.amount * 0.01 * -1;
                        nEntity.MoneyAfter = customer.Balance + nEntity.Money;
                    }
                    else
                    {
                        nEntity.Money = status.millilitre * -1;
                        nEntity.MoneyAfter = customer.Balance + nEntity.Money;
                    }
                    
                    nEntity.GroupId = GroupId;
                    MoneyChangeContract.AddEntity(nEntity);

                    customer.StateUpdateTime = null;
                    customer.StoreNo = null;
                    customer.LastReportTime = status.takeOilTime;
                    if (customer.CardType != 2)
                        customer.Balance = customer.Balance - status.amount * 0.01;
                    else
                        customer.Balance = customer.Balance + status.millilitre * -1;
                    CustomerContract.Update(customer);
                }

                var mEntity = new GetOil();
                mEntity.Id = IdMaker.NewId();
                if(customer!=null)
                    mEntity.Name = "客户[" + customer.CardNo + "]在终端[" + status.terminalNo + "]油口[" + status.oilPort + "]取油[" + status.millilitre + "]g";
                else
                    mEntity.Name = "客户在终端[" + status.terminalNo + "]油口[" + status.oilPort + "]取油[" + status.millilitre + "]g";

                if (status.voucherNo != "" && status.voucherNo!=null)
                    mEntity.Name += ",银行流水号[" + status.voucherNo + "]";
                mEntity.OilSalesSerialNumId = oilSales.Id;
                mEntity.OilSerialNumId = oilSerialNum.Id;
                mEntity.OilPort = status.oilPort;
                if(status.type==4)
                    mEntity.OperateType =0;
                else
                    mEntity.OperateType = 1;
                mEntity.OpreateTime = status.takeOilTime.ToDateTime();
                mEntity.OilOutPut = status.millilitre.ToDouble();
                mEntity.OilBefore = before;
                mEntity.OilAfter = lasted;
                mEntity.Price = status.price * 0.01;
                mEntity.Money = status.amount * 0.01;
                mEntity.LinkEntityId = null;
                if (customer != null)
                {
                    if (status.type == 4)
                    {
                        if (customer.CardType != 2)
                        {
                            mEntity.MoneyBefore = customer.Balance;
                            mEntity.MoneyAfter = customer.Balance - status.amount * 0.01;
                        }
                        else
                        {
                            mEntity.MoneyBefore = customer.Balance;
                            mEntity.MoneyAfter = customer.Balance - status.millilitre;
                        }
                    }
                    else
                    {
                        mEntity.MoneyBefore = customer.Balance;
                        mEntity.MoneyAfter = customer.Balance;
                    }
                }
                else
                {
                    mEntity.MoneyBefore = status.amount * 0.01;
                    mEntity.MoneyAfter = 0;
                }
                mEntity.MoneyChangeId = nEntity.Id;
                if (customer != null)
                    mEntity.CustomerId = customer.Id;
                else
                    mEntity.CustomerId = null;
                GetOilContract.AddEntity(mEntity);

                //var apiOperateLog = new APIOperateLog();
                //apiOperateLog.Id = IdMaker.NewId();
                //apiOperateLog.Name = "客户[" + customer.CardNo + "]在终端[" + status.terminalNo + "]油口["+status.oilPort+"]取油["+status.millilitre+"]KG";
                //apiOperateLog.OperateType = 8;
                //apiOperateLog.OilSalerNo = status.terminalNo;
                //apiOperateLog.OilPort = status.oilPort;
                //apiOperateLog.BatchNo = "";
                //apiOperateLog.OpreateTime = status.takeOilTime.ToDateTime();
                //apiOperateLog.OilOutPut = status.millilitre.ToDouble();
                //apiOperateLog.Before = before;
                //apiOperateLog.Last = lasted;
                //apiOperateLog.Price = status.amount / status.millilitre;
                //apiOperateLog.Money = status.amount;
                //apiOperateLog.UpdateTime = status.createTime.ToDateTime();
                //apiOperateLog.CustomMoney = status.amount.ToDouble();

                ////TODO ： 这里把金额一起改了把
                ////apiOperateLog.CustomMoneyLast = getOilCode.Lasted;
                ////apiOperateLog.CustomMoneyBefore = apiOperateLog.CustomMoney + apiOperateLog.CustomMoneyLast;
                //apiOperateLog.Operater = "--";
                //apiOperateLog.Token = "";
                //apiOperateLog.Status = 1;
                //apiOperateLog.OilSalesSerialNumId = oilSales.Id;
                //apiOperateLog.OilSerialNumId = oilSerialNum.Id;
                //apiOperateLog.LinkEntityId = null;
                //apiOperateLog.CustomerId = customer.Id;
                //AddEntity(apiOperateLog);

                result = scope.SaveChanges() > 0 ? OperationResult.Success : OperationResult.NoChanged;
                return result;
            }
        }

        /// <summary>
        /// 1.1.12清洗操作记录查询 根据清洗时间、清洗管道、清洗门店、清洗终端编号，查询最近一次清洗记录
        /// </summary>
        /// <param name="cleanRecordReq"></param>
        /// <returns></returns>
        public OperationResult<CleanRecordReqInfo> GetLastCleanRecord(CleanRecordReq cleanRecordReq)
        {
            var result = new OperationResult<CleanRecordReqInfo>();
            using (DbScopeFactory.CreateReadOnly())
            {
                var entity = CleanRecordContract.Entities.OrderByDescending(p => p.OperateTime).FirstOrDefault(p => p.OilSalesSerialNum.TerminalNum == cleanRecordReq.terminalNo);
                if (entity == null)
                {
                    result.ResultType = OperationResultType.QueryNull;
                    result.Message = "没有清洗记录";
                }
                else
                {
                    CleanRecordReqInfo info = new CleanRecordReqInfo();
                    info.createTime = entity.CreatedTime.ToDateTime().TimeToString("yyyy-MM-dd HH:mm:ss");
                    info.operationType = 2;
                    info.id = entity.Id;
                    info.terminalId = entity.OilSalesSerialNum.TerminalNum;
                    info.oilPort = entity.OilPort;
                    info.operation = "清洗管路";
                    info.operationTime = entity.OperateTime.ToDateTime().TimeToString("yyyy-MM-dd HH:mm:ss");
                    info.status = 1;
                    result.ResultType = OperationResultType.Success;
                    result.Data = info;
                }
                return result;
            }
        }

        /// <summary>
        /// 1.1.13清洗操作记录上传 将清洗操作记录上传到后台
        /// </summary>
        /// <param name="cleanRecordInfo"></param>
        /// <returns></returns>
        public OperationResult AddCleanRecord(CleanRecordRepInfo cleanRecordInfo)
        {
            using (var scope = DbScopeFactory.Create())
            {
                OperationResult result = OperationResult.NoChanged;

                var oilSalesSerialNum = OilSalesSerialNumContract.FirstOrDefault(p => p.TerminalNum == cleanRecordInfo.terminalNo);
                if (oilSalesSerialNum == null)
                {
                    result.ResultType = OperationResultType.ValidError;
                    result.Message = "未找到终端号[" + cleanRecordInfo.terminalNo + "]的机器";
                    return result;
                }

                var mEntity = new CleanRecord();
                mEntity.Id = IdMaker.NewId();
                mEntity.Name = "终端[" + oilSalesSerialNum.TerminalNum + "]对管路进行清洗";
                mEntity.OilSalesSerialNumId = oilSalesSerialNum.Id;
                mEntity.OilPort = cleanRecordInfo.oilPort;
                mEntity.OperateTime = System.DateTime.Now;
                CleanRecordContract.AddEntity(mEntity);


                #region APIOperateLog
                //var apiOperateLog = new APIOperateLog();
                //apiOperateLog.Name = "终端[" + oilSalesSerialNum.TerminalNum + "]取油口[" + cleanRecordInfo.oilPort + "]进行清洗";
                //apiOperateLog.OilSalerNo = oilSalesSerialNum.TerminalNum;
                //apiOperateLog.OilSalesSerialNumId = oilSalesSerialNum.Id;
                //apiOperateLog.OilPort = cleanRecordInfo.oilPort;
                //apiOperateLog.OilSerialNumId = null;
                //apiOperateLog.BatchNo = "";
                //apiOperateLog.OpreateTime = cleanRecordInfo.operationTime.ToDateTime();
                //apiOperateLog.OperateType = 13;
                //apiOperateLog.OilOutPut = 0;
                //apiOperateLog.Before = 0;
                //apiOperateLog.Last = 0;
                //apiOperateLog.Price = 0;
                //apiOperateLog.Money = 0;
                //apiOperateLog.UpdateTime = cleanRecordInfo.operationTime;
                //apiOperateLog.Token = "";
                //apiOperateLog.Operater = "test";
                //apiOperateLog.LinkEntityId = null;
                //AddEntity(apiOperateLog);
                #endregion

                result = scope.SaveChanges() > 0 ? OperationResult.Success : OperationResult.NoChanged;
                return result;
            }
        }

        /// <summary>
        /// 1.1.14管理员调试出油记录上传   管理员在登录状况下，调试出油记录上传
        /// </summary>
        /// <param name="debugOilRecord"></param>
        /// <returns></returns>
        public OperationResult AddDebugOilRecord(DebugOilRecord debugOilRecord)
        {
            using (var scope = DbScopeFactory.Create())
            {
                OperationResult result = OperationResult.NoChanged;

                var oilSalesSerialNum = OilSalesSerialNumContract.FirstOrDefault(p => p.TerminalNum == debugOilRecord.terminalNo);
                if (oilSalesSerialNum == null)
                {
                    result.ResultType = OperationResultType.ValidError;
                    result.Message = "未找到终端号[" + debugOilRecord.terminalNo + "]的机器";
                    return result;
                }

                var oilSerialNum = OilSerialNumContract.FirstOrDefault(p => p.OilSalesSerialNumId == oilSalesSerialNum.Id && p.OilPort == debugOilRecord.oilPort && p.Status == OilSerialStatus.OnSale);
                if (oilSerialNum == null)
                {
                    result.ResultType = OperationResultType.ValidError;
                    result.Message = "未找到此油口所属的油";
                    return result;
                }

                var mEntity = new GetOil();
                mEntity.Id = IdMaker.NewId();
                mEntity.Name = "终端[" + oilSalesSerialNum.TerminalNum + "]取油口[" + debugOilRecord.oilPort + "]调试出油[" + debugOilRecord.millilitre + "]g";
                mEntity.OilSalesSerialNumId = oilSalesSerialNum.Id;
                mEntity.OilSerialNumId = oilSerialNum.Id;
                mEntity.OilPort = debugOilRecord.oilPort;
                mEntity.OperateType =3;
                mEntity.OpreateTime = debugOilRecord.createTime.ToDateTime();
                mEntity.OilOutPut = debugOilRecord.millilitre;
                mEntity.OilBefore = oilSerialNum.LastOil;
                mEntity.OilAfter = oilSerialNum.LastOil - debugOilRecord.millilitre;
                mEntity.Price =0.01 * debugOilRecord.amount / debugOilRecord.millilitre * 500;
                mEntity.Money = debugOilRecord.amount * 0.01;
                mEntity.LinkEntityId = null;
                mEntity.MoneyBefore = 0;
                mEntity.MoneyAfter = 0;
                mEntity.MoneyChangeId = null;
                mEntity.CustomerId = null;
                GetOilContract.AddEntity(mEntity);

                //#region APIOperateLog
                //var apiOperateLog = new APIOperateLog();
                //apiOperateLog.Name = "终端[" + oilSalesSerialNum.TerminalNum + "]取油口[" + debugOilRecord.oilPort + "]调试出油["+debugOilRecord.millilitre+"]KG";
                //apiOperateLog.OilSalerNo = oilSalesSerialNum.TerminalNum;
                //apiOperateLog.OilSalesSerialNumId = oilSalesSerialNum.Id;
                //apiOperateLog.OilPort = debugOilRecord.oilPort;
                //apiOperateLog.OilSerialNumId = oilSerialNum.Id;
                //apiOperateLog.BatchNo = "";
                //apiOperateLog.OpreateTime = debugOilRecord.createTime.ToDateTime();
                //apiOperateLog.OperateType = 14;
                //apiOperateLog.OilOutPut = debugOilRecord.millilitre;
                //apiOperateLog.Before = oilSerialNum.LastOil;
                //apiOperateLog.Last = oilSerialNum.LastOil - debugOilRecord.millilitre;
                //if (apiOperateLog.Last < 0)
                //    apiOperateLog.Last = 0;
                //apiOperateLog.Price = debugOilRecord.amount/debugOilRecord.millilitre;
                //apiOperateLog.Money = debugOilRecord.amount;
                //apiOperateLog.UpdateTime = debugOilRecord.createTime.ToDateTime();
                //apiOperateLog.Token = "";
                //apiOperateLog.Operater = "test";
                //apiOperateLog.LinkEntityId = null;
                //AddEntity(apiOperateLog);
                //#endregion

                oilSerialNum.LastOil = oilSerialNum.LastOil - debugOilRecord.millilitre;
                OilSerialNumContract.UpdateEntity(oilSerialNum);

                result = scope.SaveChanges() > 0 ? OperationResult.Success : OperationResult.NoChanged;
                return result;
            }
        }

        /// <summary>
        /// 1.1.15 状态上报
        /// </summary>
        /// <param name="statusInfo"></param>
        /// <returns></returns>
        public OperationResult AddStatusInfo(StatusInfo statusInfo)
        {
            using (var scope = DbScopeFactory.Create())
            {
                OperationResult result = OperationResult.NoChanged;

                var oilSalesSerialNum = OilSalesSerialNumContract.FirstOrDefault(p => p.TerminalNum == statusInfo.terminalNo);
                if (oilSalesSerialNum == null)
                {
                    result.ResultType = OperationResultType.ValidError;
                    result.Message = "未找到终端号[" + statusInfo.terminalNo + "]的机器";
                    return result;
                }

                #region APIOperateLog
                var apiOperateLog = new APIOperateLog();
                apiOperateLog.OilSalerNo = oilSalesSerialNum.TerminalNum;
                apiOperateLog.OilSalesSerialNumId = oilSalesSerialNum.Id;
                apiOperateLog.OilPort =0;
                apiOperateLog.OilSerialNumId = null;
                apiOperateLog.BatchNo = "";
                apiOperateLog.OpreateTime = System.DateTime.Now;
                apiOperateLog.OperateType = 15;
                apiOperateLog.OilOutPut = 0;
                apiOperateLog.Before = 0;
                apiOperateLog.Last = 0;
                apiOperateLog.Price = 0;
                apiOperateLog.Money = 0;
                apiOperateLog.UpdateTime = System.DateTime.Now;
                apiOperateLog.Token = "";
                apiOperateLog.Operater = "test";
                apiOperateLog.LinkEntityId = null;
                apiOperateLog.OilState = statusInfo.oilState;
                apiOperateLog.DoorState = statusInfo.doorState;
                apiOperateLog.PipingState = statusInfo.pipingState;
                apiOperateLog.BaffleState = statusInfo.baffleState;

                AddEntity(apiOperateLog);
                #endregion
                result = scope.SaveChanges() > 0 ? OperationResult.Success : OperationResult.NoChanged;
                return result;
            }
        }
    }
}
